package com.yidu.qianfeng.service.impl;

import com.yidu.qianfeng.common.util.JsonMsgUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: 刘宇轩
 * @Description: 注销账号处理类
 * @Date Created in 16:10 2021/4/19
 * @since 1.0
 */

@Component
public class QfLogoutSuccessHandler implements LogoutSuccessHandler {

    /**
     * 注销成功处理
     * @param request 请求对象
     * @param response 响应对象
     * @param authentication 验证对象
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        //设置响应编码
        response.setCharacterEncoding("UTF-8");
        //设置请求头
        response.setHeader("contentType", "text/json; charset=utf-8");
        //获取writer输出流
        PrintWriter writer = response.getWriter();
        //生成json回应消息
        String msg = JsonMsgUtil.createJsonMessage(1, "退出成功", "退出成功");
        //输出write方法
        writer.write(msg);
        //刷星输出流
        writer.flush();
        //关闭输出流
        writer.close();
    }
}
